$(function () {
    $("#charge_datagrid").datagrid({
        url:'/charge/query.do',
        fit:true,
        fitColumns:true,
        striped:true,
        pageSize:5,
        pageList:[5,10,15],
        columns:[
            [
                {field:'cardNumber',title:'卡号',align:'center',width:100},
                {field:'operateWay',title:'操作方式',width:100,align:'center',formatter:function (value) {
                        return value ? '充值':'扣费'}},
                {field:'payType',title:'支付类型',align:'center',width:100,formatter:function (payType) {
                        if(payType==0){
                            return '现金';
                        }else if(payType==1){
                            return '现金';
                        }else if (payType==2){
                            return '支付宝';
                        }else if(payType==3){
                            return '微信';
                        }
                    }},
                {field:'oldBalance',title:'变动前金额',align:'center',width:100},
                {field:'chargeAmount',title:'充值金额',align:'center',width:100},
                {field:'newBalance',title:'变动后金额',align:'center',width:100},
                {field:'chargeDate',title:'充值日期',align:'center',width:100},
                {field:'remark',title:'充值备注',align:'center',width:100},
            ]
        ],
        pagination:true,
        rownumbers:true,
        singleSelect:true,
    });
    $("#chargeAmount").textbox({
        onChange:function (newValue, oldValue) {
            //获取充值金额和赠送金额
            var chargeAmount = parseFloat($("#chargeAmount").textbox('getValue')||0);
            if (chargeAmount>=100){
                $("#creditAmount").textbox('setValue',chargeAmount*0.1);
            }
            var creditAmount = parseFloat($("#creditAmount").textbox('getValue'));
            //计算合计充值
            var amount = chargeAmount + creditAmount;
            //设置值
            $("#totalAmount").text('¥'+amount);
        }
    })
    $("#member_datagrid").datagrid({
        url:'/memberInfo/query.do',
        fit:true,
        fitColumns:true,
        striped:true,
        toolbar:'#member',
        queryParams: {
            page: 1,
            rows: 100
        },
        columns:[
            [
                {field:'name',title:'会员姓名',align:'center',width:100},
                {field:'cardnumber',title:'会员卡号',align:'center',width:100},
                {field:'tel',title:'电话号码',align:'center',width:100},
                {field:'rankId',title:'会员等级',align:'center',width:100},
                {field:'balance',title:'会员余额',align:'center',width:100},
            ]
        ],
        singleSelect:true,
        onClickRow:function (index, row) {
            loadMember(row);
        }
    });
})
function query() {
    var keyword = $("[name=keyword]").val();
    $("#member_datagrid").datagrid('reload',{
        keyword:keyword,
        page: 1,
        rows: 100
    })
}
function loadMember(row) {
    console.log(row);
    $("#member_form").form('load',row);

    $("#rank").textbox('reset');
   // $("#rank").textbox('setValue',row.rank.name);

}
function charge() {
    var cardNumber = $("#cardNumber").textbox('getValue');
    console.log(cardNumber);
    if (!cardNumber) {
        $.messager.alert('温馨提示', '请输入要充值的会员!', 'warning');
        return;
    };
    var chargeAmount = $("#chargeAmount").textbox('getValue');
    if (!chargeAmount) {
        $.messager.alert('温馨提示', '请输入要充值的金额!', 'warning');
        return;
    };
    var name = $("#name").textbox('getValue');
    var totalAmount = $("#totalAmount").text();
    $.messager.confirm('确认对话框', '您确认要为会员'+"<"+name+">"+'充值'+totalAmount+'吗？', function (r) {
        if (r) {
            //ajax请求
            $("#charge_form").form('submit', {
                url: '/charge/insert.do',
                //提交之前添加参数
                onSubmit: function (param) {
                    //获取卡号
                    var memberId = $("#memberId").val();
                    console.log(memberId);
                    param['memberId'] = memberId;
                    //获取
                    var balance = $("#balance").textbox('getValue');
                    param['balance'] = balance;
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('提示', '充值成功', 'info', function () {
                            //清除表单数据
                            $("#charge_form").form('clear');
                            $("#member_form").form('clear');
                            //刷新数据
                            $("#charge_datagrid").datagrid('reload');
                            $("#member_datagrid").datagrid('reload');
                        });
                    }
                }
            });
        }
    });
}